import axios from 'axios'

export function useApi<REQ, RES>(path: string) {
  async function get(req?: REQ): Promise<RES> {
    const { data } = await axios.get<RES>(path, { params: req })
    return data
  }
  async function post(req?: Partial<REQ>): Promise<RES> {
    const { data } = await axios.post<RES>(path, req)
    return data
  }
  async function patch(req?: Partial<REQ>): Promise<RES> {
    const { data } = await axios.patch<RES>(path, req)
    return data
  }
  async function put(req?: Partial<REQ>): Promise<RES> {
    const { data } = await axios.put<RES>(path, req)
    return data
  }
  async function remove(req?: REQ): Promise<RES> {
    const { data } = await axios.delete<RES>(path, { params: req })
    return data
  }

  async function download(req?: Partial<REQ>) {
    await axios.get(path, { params: req, responseType: 'blob' })
  }

  async function postDownload(req?: Partial<REQ>) {
    await axios.post(path, req, { responseType: 'blob' })
  }

  return { get, post, patch, put, remove, download, postDownload }
}
